package com.huonan.test.test2;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.huonan.jb.treads.ThreadPool;
import com.huonan.utils.FileUtils;
import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Jsoup;

/**
 * @Description
 * @Author lxp
 * @Date 2021/11/23 上午10:13
 * @Version V1.0
 **/
public class RmHtmls {
    private static final Logger log = LogManager.getLogger(RmHtmls.class);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = ThreadPool.getExecutorService();
        List<Future<Integer>> list = new ArrayList<>();
        String dir = "/Users/lxp/IdeaProjects/Study/MainTest/data/filtered/数据修复/comment.txt";
        List<File> fileDirs = FileUtil.loopFiles(Paths.get(dir), 2, null);
        AtomicInteger count = new AtomicInteger();
        fileDirs.forEach(x -> {
            log.info("处理文件目录：{}", x);
            List<File> fileList = FileUtil.loopFiles(x);
            fileList.forEach(f -> {
                log.info("处理文件：{}", f);
                Future<Integer> submit = executorService.submit(() -> {
                    TimeInterval interval = new TimeInterval();
                    List<String> lines = FileUtil.readLines(f, "UTF-8");
                    count.getAndIncrement();
                    StringBuffer sb = new StringBuffer();
                    lines.forEach(l -> {
                                String line = l.substring(l.indexOf("{"));
                                JSONObject obj = JSONUtil.parseObj(line);
                                System.out.println(obj);
                                String cid = obj.getStr("cid");
                                String aid = obj.getStr("aid");
                                String content = obj.getStr("content");
                                String deleteContent = Jsoup.parse(content).text().replaceAll("u3000", " ");
                                //TODO 去Unicode
                                String id;
                                String index = null;
                                if (cid != null) {
                                    id = "0_bayou_c_" + cid;
                                    //pubDate = obj.getStr("pub_time");
                                } else {
                                    id = "0_bayou_" + aid;
                                    String pub_time = obj.getStr("pub_time");
                                    //ik_sl_v2_%{month}_%{type}
                                    DateTime dateTime = DateUtil.parse(pub_time);
                                    String pubDate = DateUtil.format(dateTime, "yyyyMM");
                                    index = "ik_sl_v2_" + pubDate + "_wenda";
                                }
                                // JSONObject json = new JSONObject();
                                JSONObject json1 = JSONUtil.createObj();
                                json1.set("content", deleteContent);
                                json1.set("_index", index);
                                json1.set("_id", id);
                                // json.set("id", id);
                                // json.set("index", index);
                                // json.set("content", deleteContent);
                                String jsonStr = JSONUtil.toJsonStr(json1);
                                sb.append(jsonStr).append("\n");
                                // System.out.println(aid);
                                // System.out.println(deleteContent);
                            }
                    );
                    String outPath = "/Users/lxp/IdeaProjects/Study/MainTest/data/filtered/r_comment.txt";
                    sb.deleteCharAt(sb.length() - 1);
                    FileUtils.writeAppend(new File(outPath), sb.toString(), "UTF-8");
                    log.info("耗时：{}", interval.interval());
                    return count.get();
                });
                list.add(submit);
            });
        });
        for (Future<Integer> future : list) {
            System.out.println(future.get());
        }
        executorService.shutdown();
    }


}
